//导入
var express = require('express');
var fs = require('fs');
var path = require('path');

//创建子路由对象
var userRouter = express.Router()

// var arr = [
//     { email: '178884773@qq.com', name: '张伟', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
//     { email: '178884773@qq.com', name: '李煜', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
//     { email: '178884773@qq.com', name: '李清照', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: false },
//     { email: '178884773@qq.com', name: '李浩', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
//     { email: '178884773@qq.com', name: '大锤', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
//     { email: '178884773@qq.com', name: '子怡', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: false },
//     { email: '178884773@qq.com', name: '海洛', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
//     { email: '178884773@qq.com', name: '崽崽', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: false },
//     { email: '178884773@qq.com', name: '奶糖', phone: '13611129070', pass: '1234', ingroup: '编辑组', isadmin: true, status: false }
// ]

//req  请求对象，包含本次请求相关的信息
//res  响应请求，可以借助res向客户端返回信息
//分页
userRouter.get('/list', (req, res) => {
    var { page, count } = req.query;

    //读取JSON文件
    var filepath = path.join(__dirname, '../json/user.json')
    //读取文件路径
    var arr = fs.readFileSync(filepath)
    //初始化
    arr = arr.toString() ? JSON.parse(arr) : []

    //分页
    //slice 选取
    var newarr = arr.slice( (page-1)*count, page*count );
    //返回数据

    // console.log('收到了来自客户端的请求');
    res.send({ code: 200, msg: '请求成功', list: newarr })
})

//搜索
userRouter.get('/search', (req, res) => {
    //接收参数
    var { searchkey } = req.query;

    //读取JSON文件
    var filepath = path.join(__dirname, '../json/user.json')
    //读取文件路径
    var arr = fs.readFileSync(filepath)
    //初始化
    arr = arr.toString() ? JSON.parse(arr) : []
//过滤
    var newarr = arr.filter((item) => {
        return item.name.indexOf(searchkey) != -1

    })
    //返回数据
    res.send({ code: 200, msg: '查询成功!', list: newarr })
})

//登录
userRouter.post('/login', (req, res) => {

    var { phone, pass } = req.body;
    // console.log('收到了来自客户端的请求');
    //读取JSON文件
    var filepath = path.join(__dirname, '../json/user.json')
    //读取文件路径
    var arr = fs.readFileSync(filepath)
    //初始化
    arr = arr.toString() ? JSON.parse(arr) : []

    //做判断，判断是否在服务端的用户列表中
    var i=arr.findIndex((item,index)=>{
        return item.phone==phone&&item.pass==pass
    })

    if(i!=-1){
        //登陆成功是，可以一并返回
        res.send({code:200,msg:'登陆成功',userinfo:arr[i],token:''})
    }else{
        res.send({ code: 200, msg: '登陆成功' })
    }
  
})
//注册
userRouter.post('/register', (req, res) => {

    //获取注册参数
    var{phone}=req.body;//{phone,pass,email,name}

    //添加到JSON中
      //读取JSON文件
      var filepath = path.join(__dirname, '../json/user.json')
      //读取文件路径
      var arr = fs.readFileSync(filepath)
      //初始化
      arr = arr.toString() ? JSON.parse(arr) : []

      var i=arr.findIndex((item)=>{
        return item.phone==phone;
      })

      if(i!=-1){
        res.send({code:400,msg:'该手机号已注册，注册失败！'})
      }else{
        arr.push({...req.body,ingroup:'编辑组',isadmin:true,status:true})
        fs.writeFileSync(filepath,JSON.stringify(arr),'utf8')
        res.send({code:200,msg:'注册成功！'})
    }
})

//删除
userRouter.post( '/delete' ,(req,res)=>{
    //获取手机号
    var {phone} = req.body;

    //读取json文中的用户数据
    var filepath = path.join( __dirname, '../json/users.json' );
    var arr = fs.readFileSync(filepath);
    arr = arr.toString()?JSON.parse( arr ): []

    //在arr中查找用户
    var i = arr.findIndex((item)=>{
        return item.phone == phone;
    })
    if( i != -1 ){ 
        arr.splice( i, 1 );
        fs.writeFileSync(filepath, JSON.stringify( arr ), 'utf8' );
        //分页
        var newarr = arr.slice( 0, 2 );
        //返回删除后,第1页的新数据
        res.send({ code:200,msg:'删除成功!',list: newarr })
    }else{
        res.send({ code:400,msg:'删除失败!' })
    }
})
//导出
module.exports = userRouter;